package com.liwuji.liwujiredisdocker.config;


import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * @author liwuji
 * @date 2024/10/25
 * @desc
 **/
@Configuration
public class RedisConfig {

    @Bean
    @ConditionalOnProperty("spring.redis")
    public RedisTemplate redisTemplate(RedisConnectionFactory factory) {
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(factory);
        Jackson2JsonRedisSerializer keySerializer = new Jackson2JsonRedisSerializer(String.class);
        redisTemplate.setKeySerializer(keySerializer);
        Jackson2JsonRedisSerializer valueSerializer = new Jackson2JsonRedisSerializer(Object.class);
        redisTemplate.setValueSerializer(valueSerializer);
        return redisTemplate;
    }
}
